package com.astrob.activitys;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Build;

import com.alibaba.sdk.android.feedback.impl.FeedbackAPI;
import com.astrob.tts.BaiduTTS;
import com.astrob.util.SysUtils;
import com.nhc.database.greendao.DaoMaster;
import com.nhc.database.greendao.DaoSession;
import com.nhc.database.manager.GreenDaoContext;

import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.MapTile;
import org.osmdroid.tileprovider.tilesource.MapBoxTileSource;
import org.osmdroid.tileprovider.tilesource.MapQuestTileSource;
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.tileprovider.tilesource.XYTileSource;
import org.osmdroid.tileprovider.tilesource.bing.BingMapTileSource;

import zuo.biao.library.util.ImageLoaderUtil;
import zuo.biao.library.util.Log;

public class AppContext extends Application {
    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    public static AppContext instances;
    private Typeface iconfont;

    private static final String DATABASE_NAME = "nhc_map.db";

    @Override
    public void onCreate() {
        super.onCreate();
        instances = this;
    }

    public void init() {
        ImageLoaderUtil.init(instances);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            //4.4一下系统使用，5.0可能也可以(6.0应该不可以)，华测设备为4.4
            BaiduTTS.init(getApplicationContext());
        }
        iconfont = Typeface.createFromAsset(getAssets(), "iconfont/iconfont.ttf");
        SysUtils.getInstance().init(this);
        initOSM();
        AndroidGraphicFactory.createInstance(this);//Mapsforge


        //阿里百川（已弃用）->阿里云 移动服务-用户反馈
        //https://help.aliyun.com/document_detail/53144.html?spm=a2c4g.11186623.4.1.Cl7ANw
        FeedbackAPI.init(this, "24949757", "e6b4cdee0a9aea30bfe6415c5d47c4ca");
    }

    public static AppContext getInstances() {
        return instances;
    }

    public Typeface getIconfont() {
        return iconfont;
    }

    /**
     * 设置greenDao
     * 数据库路径： /BHCMapper/sqlite/greendao/nhc_map.db
     */
    public void setDatabase() {
        // 通过 DaoMaster 的内部类 DevOpenHelper，你可以得到一个便利的 SQLiteOpenHelper 对象。
        // 可能你已经注意到了，你并不需要去编写「CREATE TABLE」这样的 SQL 语句，因为 greenDAO 已经帮你做了。
        // 注意：默认的 DaoMaster.DevOpenHelper 会在数据库升级时，删除所有的表，意味着这将导致数据的丢失。
        // 所以，在正式的项目中，你还应该做一层封装，来实现数据库的安全升级。
        mHelper = new DaoMaster.DevOpenHelper(new GreenDaoContext(getApplicationContext()), DATABASE_NAME, null);
        db = mHelper.getWritableDatabase();
        // 注意：该数据库连接属于 DaoMaster，所以多个 Session 指的是相同的数据库连接。
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }


    private void initOSM() {
        Configuration.getInstance().setUserAgentValue(getPackageName());
        BingMapTileSource.retrieveBingKey(this);
        final BingMapTileSource source = new BingMapTileSource(null);
        new Thread(new Runnable() {
            @Override
            public void run() {
                source.initMetaData();
            }
        }).start();
        source.setStyle(BingMapTileSource.IMAGERYSET_AERIALWITHLABELS);
        TileSourceFactory.addTileSource(source);

        final BingMapTileSource source2 = new BingMapTileSource(null);
        new Thread(new Runnable() {
            @Override
            public void run() {
                source2.initMetaData();
            }
        }).start();
        source2.setStyle(BingMapTileSource.IMAGERYSET_ROAD);
        TileSourceFactory.addTileSource(source2);

        //FIXME need a key for this TileSourceFactory.addTileSource(TileSourceFactory.CLOUDMADESMALLTILES);

        //FIXME need a key for this TileSourceFactory.addTileSource(TileSourceFactory.CLOUDMADESTANDARDTILES);

        //the sample app a few additional tile sources that we have api keys for, so add them here
        //this will automatically show up in the tile source list
        //FIXME this key is expired TileSourceFactory.addTileSource(new HEREWeGoTileSource(getApplicationContext()));
        TileSourceFactory.addTileSource(new MapBoxTileSource(getApplicationContext()));
        TileSourceFactory.addTileSource(new MapQuestTileSource(getApplicationContext()));

        TileSourceFactory.addTileSource(GoogleSat);
        TileSourceFactory.addTileSource(GoogleRoads);
        TileSourceFactory.addTileSource(GoogleHybrid);
        TileSourceFactory.addTileSource(GoogleNormal);
    }

    private static final OnlineTileSourceBase GoogleNormal = new XYTileSource("Google-GoogleNormal",
            2, 19, 256, ".png", new String[]{
            "http://mt0.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2",
            "http://mt1.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2",
            "http://mt2.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2",
            "http://mt3.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2",
    }) {
        @Override
        public String getTileURLString(MapTile aTile) {
            return getBaseUrl() + "&x=" + aTile.getX() + "&y=" + aTile.getY() + "&z=" + aTile.getZoomLevel();
        }
    };


    private static final OnlineTileSourceBase GoogleHybrid = new XYTileSource("Google-Hybrid",
            0, 19, 256, ".png", new String[]{
            "http://mt0.google.com",
            "http://mt1.google.com",
            "http://mt2.google.com",
            "http://mt3.google.com",
    }) {
        @Override
        public String getTileURLString(MapTile aTile) {
            return getBaseUrl() + "/vt/lyrs=y&x=" + aTile.getX() + "&y=" + aTile.getY() + "&z=" + aTile.getZoomLevel();
        }
    };

    private static final OnlineTileSourceBase GoogleSat = new XYTileSource("Google-Sat",
            0, 19, 256, ".png", new String[]{
            "http://mt0.google.com",
            "http://mt1.google.com",
            "http://mt2.google.com",
            "http://mt3.google.com",

    }) {
        @Override
        public String getTileURLString(MapTile aTile) {
            return getBaseUrl() + "/vt/lyrs=s&x=" + aTile.getX() + "&y=" + aTile.getY() + "&z=" + aTile.getZoomLevel();
        }
    };

    private static final OnlineTileSourceBase GoogleRoads = new XYTileSource("Google-Roads",
            0, 19, 256, ".png", new String[]{
            "http://mt0.google.com",
            "http://mt1.google.com",
            "http://mt2.google.com",
            "http://mt3.google.com",

    }) {
        @Override
        public String getTileURLString(MapTile aTile) {
            return getBaseUrl() + "/vt/lyrs=m&x=" + aTile.getX() + "&y=" + aTile.getY() + "&z=" + aTile.getZoomLevel();
        }
    };


}
